package com.fanfanwang.ffw.app.receiver;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.ui.my.MyRingActivity;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Vibrator;
import android.provider.AlarmClock;
import android.sax.StartElementListener;
import android.widget.Toast;

public class OneShotAlarm extends BroadcastReceiver{
	private NotificationManager manager;  
	private Notification notification;  
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		if(SharedPreferencesUtil.getInstance(context).getIsreceivealarmclock())//如果需要提醒在做一下操作。
		{
			ToastAlone.showToast(context, context.getResources().getString(R.string.order_yuyue_time_start), Toast.LENGTH_LONG).show();
			notification(context, "");
			if(SharedPreferencesUtil.getInstance(context).getRemindvibration())
			{
				//开启震动	
				long [] pattern = {100,500,100,500};   // 停止 开启 停止 开启   
				Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);  
				vibrator.vibrate(pattern,-1); 
			}
			int pos=SharedPreferencesUtil.getInstance(context).getRemindbellpos();
			if (pos == 0) {

				Uri uri = RingtoneManager.getActualDefaultRingtoneUri(
						context, RingtoneManager.TYPE_NOTIFICATION);
				RingtoneManager.getRingtone(context, uri).play();
			}else{
				try {

					RingtoneManager rm = new RingtoneManager(context);
					rm.setType(RingtoneManager.TYPE_NOTIFICATION);
					rm.getCursor();
					rm.getRingtone(pos - 1).play();

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	private void notification(Context context,String content) {  
		// 获取系统的通知管理器  
		manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);  
		notification = new Notification(R.drawable.ic_launcher, content,  
				System.currentTimeMillis());  
		notification.defaults = Notification.DEFAULT_ALL; // 使用默认设置，比如铃声、震动、闪灯  
		notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后，消息自动在通知栏自动消失  左右滑动可以消失，
//		notification.flags |= Notification.FLAG_NO_CLEAR;// 点击通知栏的删除，消息不会依然不会被删除  
//		Intent intent = new Intent(context,  
//				GlyMainActivity.class);
//		intent.putExtra("isNotify", true);
		//  该语句的作用是定义了一个不是当即显示的activity，只有当用户拉下notify显示列表，并且单击对应的项的时候，才会触发系统跳转到该activity.
//		pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);  
		//在此处设置在nority列表里的该norifycation得显示情况。
		notification.setLatestEventInfo(context, "停车位预约提醒", "预约的停车位提醒时间到了。", null);  

		// 将消息推送到状态栏  
		manager.notify(0, notification);  

	}


}
